//DO NOT CHANGE THIS PACKAGE
package coursework3;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cw3interfaces.SkillSorterInterface;




//DO NOT CHANGE THIS NAME
public class SkillSorter implements SkillSorterInterface{
    private ArrayList<CommunityGroup> myGroups = new ArrayList<>();
    
    
    private char[] skills = new char[] {'A','B','C','D','E'};
    
    //COMPLETE THIS CLASS
    public SkillSorter() {
    	CommunityGroup group1 = new CommunityGroup("CommunityA");
    	CommunityGroup group2 = new CommunityGroup("CommunityB");
    	CommunityGroup group3 = new CommunityGroup("CommunityC");
    	CommunityGroup group4 = new CommunityGroup("CommunityD");
    	CommunityGroup group5 = new CommunityGroup("CommunityE");
    	
    	myGroups.add(group1);
    	myGroups.add(group2);
    	myGroups.add(group3);
    	myGroups.add(group4);
    	myGroups.add(group5);
    }
    
    
//these public methods need to form the interface 
// DO NOT CHANGE ANY OF THESE METHOD NAMES, RETURN VALUES, OR ARGUMENTS
    @Override
    public void addVolunteer(Volunteer vol){
        //add a volunteer to a Community Group USING YOUR SORTING ALGORITHM
        //COMPLETE CODE HERE
        String skills = vol.getSkillSet();
        char cskill = maxChar(skills);
        CommunityGroup group = check(cskill);
        group.getVolunteers().add(skills);
        group.statistics();
    }
    
    private CommunityGroup check(char skill) {
    	int index = -1;
    	for(char c : skills) {
    		index ++;
    		if(c == skill) {
    			break;
    		}
    	}
    	int minnum = 1500;
    	CommunityGroup rg = null;
    	for( CommunityGroup g : myGroups ) {
    		if( g.skillNumber[index] == 0 ) {
    			return g;
    		}
    		if(g.skillNumber[index] < minnum) {
    			minnum = g.skillNumber[index];
    			rg = g;
    		}
    	}
    	return rg;
    }
    
    public char maxChar(String skills) {
    	int maxindex = 0;
    	char flag = ' ';
    	char[] sks = skills.toCharArray();
    	Map<String,Integer> m = new HashMap<>();
    	for(char sk : sks) {
    		if(m.containsKey(String.valueOf(sk))) {
    			Integer a = m.get(String.valueOf(sk));
    			m.put(String.valueOf(sk), a++ );
    			if( a > maxindex) {
    				maxindex = a;
    				flag = sk;
    			}
    		}else {
    			m.put(String.valueOf(sk), 1);
    		}
    	}
    	if(maxindex > 0) {
    		return flag;
    	}else {
    		int index = (int)(Math.random()*sks.length);
    		return sks[index];
    	}
    }
    
    @Override
    public void moveVolunteer(String skillSet, CommunityGroup from, CommunityGroup to){
        //move a volunteer with this skillset (eg AAA, BCD) from one CommunityGroup to another
        //COMPLETE CODE HERE
    	for(String skill : from.getVolunteers()) {
    		if(skill.equals(skillSet)) {
    			to.getVolunteers().add(skillSet);
    		}
    	}
    	from.getVolunteers().remove(skillSet);
    	from.statistics();
    	to.statistics();
    }
    
    @Override
    public void deleteVolunteer(String skillSet, CommunityGroup from){
        //delete a volunteer with this skillset from this CommunityGroup
        //COMPLETE CODE HERE
    	from.getVolunteers().remove(skillSet);
    	from.statistics();
    }
    
    @Override
    public void deleteAllVolunteers(){
        // delete all volunteers from all CommunityGroups
        //COMPLETE CODE HERE
    	for(CommunityGroup cg : myGroups) {
    		cg.getVolunteers().clear();
    		cg.statistics();
    	}
    }

    @Override
    public ArrayList<CommunityGroup> getCommunityGroups(){
        //return an ArrayList of all this application's CommunityGroups
        return myGroups;
    }
    
   
   
}
